package com.jcark.utilscode.ext

import android.text.TextUtils
import android.view.View
import android.widget.TextView
import androidx.core.content.ContextCompat

fun TextView.getTextWidth() = paint.measureText(text.toString())

fun TextView.setViewText(text: String?, isGoneWhenEmpty: Boolean = true) {
    if (TextUtils.isEmpty(text)) {
        if (isGoneWhenEmpty) {
            this.visibility = View.GONE
        }
    } else {
        if (this.visibility != View.VISIBLE) {
            this.visibility = View.VISIBLE
        }
        this.text = text
    }
}

/**
 * 设置右侧图标
 */
fun TextView.setRightDrawable(textStr: String? = null, rightDrawableResId: Int? = null) {
    if (!TextUtils.isEmpty(textStr)) {
        this.text = textStr
    }

    this.setCompoundDrawablesWithIntrinsicBounds(
        null,
        null,
        if (null != rightDrawableResId) ContextCompat.getDrawable(context, rightDrawableResId) else null,
        null
    )
}